package com.li.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.li.constant.MessageConstant;
import com.li.entity.Result;
import com.li.pojo.OrderSetting;
import com.li.service.OrderSettingService;
import com.li.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    OrderSettingService orderSettingService;

    //预约设置上传
    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){

        try {
            //获取excel文件数据
            List<String[]> list = POIUtils.readExcel(excelFile);
            if(list!=null&&list.size()>0){
                ArrayList<OrderSetting> orderSettings = new ArrayList<>();

                for (String[] strings : list) {

                    OrderSetting orderSetting = new OrderSetting(Str2Date(strings[0]), Integer.parseInt(strings[1]));
                    orderSettings.add(orderSetting);
                }

                orderSettingService.add(orderSettings);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    @RequestMapping("/getOrdersettingByMonth")
    public Result getOrdersettingByMonth(String date){

        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);

            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        }catch (Exception e){
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/editNumberByDate")
    public Result editNumberByMonth(@RequestBody OrderSetting orderSetting){

        try {
            orderSettingService.editNumberByDate(orderSetting);

            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
    public Date Str2Date(String str){
        //字符串转换为日期
        String[] split = str.split("/");
        if(split[1].length()==1){
            split[1]="0"+split[1];
        }

        if(split[2].length()==1){
            split[2]="0"+split[2];
        }

        return new Date(split[0]+"/"+split[1]+"/"+split[2]);
    }
}
